	var classes = {
		init : function() {
			classes.classesGrid = $('#classesGrid');
			classes.menu = $('#menu');
			classes.classesAddDialog = $('#classesAddDialog');
			classes.id = $('#id');
			classes.name = $('#name');
			classes.classesAddForm = $('#classesAddForm');
			classes.classesUpdateForm = $('#classesUpdateForm');
			classes.menuUpdate = $('#menuUpdate');
			classes.classesUpdateDialog = $('#classesUpdateDialog');
			
			classes.classesGrid.datagrid({
			    url: '<%=request.getContextPath()%>/ClassesServlet?command=listClasses',
			    fixed: true,
			    fitColumns: true,
			    striped: true,
			    rownumbers: true,
			    pagination: true,
			    pageSize: 20,
			    pageList: [10, 20, 30, 40, 50],
			    pageNumber: 1,
			    toolbar: '#classesToolbar',
			    columns:[[
					{field:'id', checkbox: true},
					{field:'name', title:'班级名称', width:100, align:'center'},
					{field:'course__name', title:'关联课程', width:50, align:'center', sortable: true},
					{field:'operate_time', title:'创建时间', width:100, align:'center'},
					{field:'state', title:'状态', width:100, align:'center'},
					{field:'memo', title: '备注', formatter: classes.row.showGender, align:'center'},
					{field:'operate', title: '操作', formatter: classes.row.showState, align:'center'}
			    ]],
				onDblClickRow: function(rowIndex, rowData) {
					classes.toolbar.showUpdateDialog(rowData.id);
				}
			});
		},
		toolbar: {
			showCombotree: function() {
				classes.menu.combotree({
					url: '<%=request.getContextPath()%>/ClassesServlet?command=listMenu',
					lines: true,
					multiple: true,
					checkbox: true,
					onlyLeafCheck: true,
					onLoadSuccess: function(node, data) {
						$(this).tree('expandAll')
					}
				});
			},
			showAddDialog: function() {
				classes.toolbar.showCombotree();
				classes.classesAddDialog.dialog('open');
				classes.code.focus();
				// 坑，只能使用id选择器
				classes.age.numberspinner('setValue', 20);
			},
			hiddenAddDialog: function() {
				classes.classesAddDialog.dialog('close');
				classes.toolbar.resetAddForm();
			},
			resetAddForm: function() {
				classes.classesAddForm.form('reset');
			},
			add: function() {
				if (classes.classesAddForm.form('validate')) {
					$.messager.progress({
						text : '正在处理...'
					});
					$.post('<%=request.getContextPath()%>/ClassesServlet?command=addClasses', classes.classesAddForm.serialize(), function(data) {
						if (data) {
							$.messager.show({
								title: '系统提示',
								msg: '新增成功！',
								timeout: 3000,
								showType: 'slide'
							});
							classes.toolbar.resetAddFrom();
							classes.classesGrid.datagrid('reload');
						} else {
							$.messager.alert('系统提示', '新增失败！', 'error')
						}
						$.messager.progress('close');
						classes.toolbar.hiddenAddDialog();
					});
				}
			},
			showUpdateDialog: function(id) {
				classes.toolbar.showCombotree();
				var getData = function(v) {
					$.messager.progress({
						text : '正在处理...'
					});
					$.post('<%=request.getContextPath()%>/ClassesServlet?command=getClasses', {id : v}, function(data) {
						if (data) {
							classes.classesUpdateForm.form('load', {
								id: data.id,
								name: data.name,
								courseId: date.courseId,
								courseName: data.courseName,
								operateTime: data.operateTime,
								state: data.state,
								memo: data.memo
							});
							// 测试默认选中管理员管理,求职者管理
							var checkedItems = '管理员管理,求职者管理'.split(',');
							classes.menuUpdate.combotree({
								url: '<%=request.getContextPath()%>/TestServlet?command=listMenu',
								lines: true,
								multiple: true,
								checkbox: true,
								onlyLeafCheck: true,
								onLoadSuccess: function(node, data) {
									var that = this;
									$(that).tree('expandAll')
									if (data) {
										$(data).each(function(index, value) {
											if ($.inArray(value.text, checkedItems) != -1) {
												$(that).tree('check', value.target);
											}
										});
									}
								}
							});
							$('#classesUpdateDialog').dialog('open');
						} else {
							$.messager.alert('系统提示', '获取信息失败！', 'error')
						}
						$.messager.progress('close');
					}, 'json');
				}
				if (id) {
					getData(id);
				} else {
					var row = classes.classesGrid.datagrid('getSelections');
					if (row.length == 1) {
						getData(row[0].id);
					} else {
						$.messager.show({
							title: '系统提示',
							msg: '请选择一行数据进行修改！',
							timeout: 3000,
							showType: 'slide'
						});	
					}
				}
			},
			hiddenUpdateDialog: function() {
				$('#classesUpdateDialog').dialog('close');
				classes.toolbar.resetUpdateFrom();
			},
			resetUpdateFrom: function() {
				classes.classesUpdateForm.form('reset');
			},
			update: function() {
				if (classes.classesUpdateForm.form('validate')) {
					$.messager.progress({
						text : '正在处理...'
					});
					$.post('<%=request.getContextPath()%>/ClassesServlet?command=updateClasses', $("#classesUpdateForm").serialize(), function(data) {
						if (data == 1) {
							$.messager.show({
								title: '系统提示',
								msg: '修改成功！',
								timeout: 3000,
								showType: 'slide'
							});
							classes.toolbar.resetUpdateFrom();
							classes.classesGrid.datagrid('reload');
						} else {
							$.messager.alert('系统提示', '修改失败！', 'error');
						}
						$.messager.progress('close');
						classes.toolbar.hiddenUpdateDialog();
					});
				}
			},
			remove: function() {
				var row = classes.classesGrid.datagrid('getSelections');
				if (row.length > 0) {
					var ids = [];
					$.each(row, function(i, v) {
						ids.push(v.id);
					});
					$.post('<%=request.getContextPath()%>/ClassesServlet?command=deleteClasses', {ids: ids.join(',')}, function(data) {
						if (data == 1) {
							$.messager.show({
								title: '系统提示',
								msg: '删除成功！',
								timeout: 3000,
								showType: 'slide'
							});
							classes.classesGrid.datagrid('reload');
						} else {
							$.messager.alert('系统提示', '删除失败！', 'error')
						}
						$.messager.progress('close');
					});
				} else {
					$.messager.show({
						title: '系统提示',
						msg: '请选择数据进行删除！',
						timeout: 3000,
						showType: 'slide'
					});	
				}
			},
			redo: function() {
				classes.classesGrid.datagrid('unselectAll');
			}
		},
		row : {
			showState: function(value, row, index) {
				var id = row.id;
				var state = row.state;
				if (!value) {
					value = state == 1 ? '有效' : '无效';
				}
				var operations = [];
				operations.push('<a href="javascript:void(0)" onclick="classes.row.updateGender(\'' + id + '\',  \'' + value + '\',  \'' + index + '\')">' + value + '</a>');
				return operations.join('');
			},
			updateState: function(id, value, index) {
				$.messager.confirm('Confirm', 'Are you sure to update this item?', function(result){
					if (result){
						$.messager.show({
							title: '系统提示',
							msg: '状态修改成功！',
							timeout: 3000,
							showType: 'slide'
						});
 						classes.classesGrid.datagrid('updateRow', {
							index: index,
							row: {
								operate: value == '有效' ? '无效' : '有效'
							}
						}); 
					} else {
						$.messager.alert('系统提示', '状态修改失败！', 'error')
					}
				});
			}
		}
	};
	
	$(function(){
		classes.init();
	});
